// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_CHK_QUALITY_CHECKING_ITEM
// * CreateDate    : 2011-11-23 11:20:26
// * Creater       : CodeSmith
// * LastChangeDate: 2011-11-23 11:20:26
// * LastChanger   : 2011-11-23 11:20:26
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESOL.Common.Entity;
using Suzsoft.Smart.Data;
using System.Data;
using ESOL.Common;

namespace ESOL.Business.Chk
{
/// <summary>
    /// ChkQualityCheckingItem
    /// </summary>
    public partial class ChkQualityCheckingItemBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objChkQualityCheckingItemEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ChkQualityCheckingItemEntity GetChkQualityCheckingItem(ChkQualityCheckingItemEntity objChkQualityCheckingItemEntity)
        {
            return DataAccess.SelectSingle<ChkQualityCheckingItemEntity>(objChkQualityCheckingItemEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objChkQualityCheckingItemEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertChkQualityCheckingItem(ChkQualityCheckingItemEntity objChkQualityCheckingItemEntity)
        {
            return DataAccess.Insert(objChkQualityCheckingItemEntity);
        }

        public static bool InsertChkQualityCheckingItem(List<ChkQualityCheckingItemEntity> objChkQualityCheckingItemEntityList)
        {
            return DataAccess.Insert(objChkQualityCheckingItemEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objChkQualityCheckingItemEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateChkQualityCheckingItem(ChkQualityCheckingItemEntity objChkQualityCheckingItemEntity)
        {
            return DataAccess.Update(objChkQualityCheckingItemEntity);
        }

        public static bool UpdateChkQualityCheckingItem(List<ChkQualityCheckingItemEntity> objChkQualityCheckingItemEntityList)
        {
            return DataAccess.Update<ChkQualityCheckingItemEntity>(objChkQualityCheckingItemEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objChkQualityCheckingItemEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteChkQualityCheckingItem(ChkQualityCheckingItemEntity objChkQualityCheckingItemEntity)
        {
            return DataAccess.Delete(objChkQualityCheckingItemEntity);
        }

        public static bool DeleteChkQualityCheckingItem(List<ChkQualityCheckingItemEntity> objChkQualityCheckingItemEntityList)
        {
            return DataAccess.Delete<ChkQualityCheckingItemEntity>(objChkQualityCheckingItemEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetChkQualityCheckingItemAll()
        {
            return DataAccess.SelectAllDataSet<ChkQualityCheckingItemEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ChkQualityCheckingItemEntity> GetChkQualityCheckingItemAllList()
        {
            return DataAccess.SelectAll<ChkQualityCheckingItemEntity>();
        }
        #endregion

        #region Function
        public static ChkQualityCheckingItemEntity GetChkQualityCheckingItemByID(string ID)
        {
            if (string.IsNullOrEmpty(ID))
                return null;
            ChkQualityCheckingItemEntity m = new ChkQualityCheckingItemEntity();
            m.CheckingItemId = ID;
            return DataAccess.SelectSingle<ChkQualityCheckingItemEntity>(m);
        }

        public static DataTable GetDtCheckingItemByCheckingID(string CheckingID)
        {
            if (string.IsNullOrEmpty(CheckingID))
                return null;

            string Sql = @"SELECT * FROM
                (SELECT CQCI.*,MP.PRODUCT_NAME,MP.PRODUCT_CODE,MP.MATERIAL,MP.DESCRIPTION,NG_QTY/RELATED_ITEM_QTY AS NG_RATE 
                FROM CHK_QUALITY_CHECKING_ITEM CQCI
                INNER JOIN CHK_QUALITY_CHECKING CQC ON CQCI.CHECKING_ID=CQC.CHECKING_ID
                LEFT JOIN PUR_PURCHASE_ARRIVED_ITEM PPAI ON CQCI.RELATED_ITEM_ID=PPAI.ARRIVED_ITEM_ID
                LEFT JOIN PUR_PURCHASE_ORDER_ITEM PPOI ON PPAI.PURCHASE_ITEM_ID=PPOI.PURCHASE_ITEM_ID
                LEFT JOIN MST_PRODUCT MP ON PPOI.PRODUCT_ID=MP.PRODUCT_ID
                WHERE CQC.CHECKING_ORDER_TYPE='QCOT01' AND CQCI.RELATED_ITEM_QTY!=0
                UNION ALL
                SELECT CQCI.*,MP.PRODUCT_NAME,MP.PRODUCT_CODE,MP.MATERIAL,MP.DESCRIPTION,NG_QTY/RELATED_ITEM_QTY AS NG_RATE 
                FROM CHK_QUALITY_CHECKING_ITEM CQCI
                INNER JOIN CHK_QUALITY_CHECKING CQC ON CQCI.CHECKING_ID=CQC.CHECKING_ID
                LEFT JOIN PROD_PLAN_ORDER_ITEM PPOI ON CQCI.RELATED_ITEM_ID=PPOI.PLAN_ORDER_ITEM_ID
                LEFT JOIN MST_PRODUCT MP ON PPOI.PRODUCT_ID=MP.PRODUCT_ID
                WHERE CQC.CHECKING_ORDER_TYPE='QCOT02' ) AS ALL_LIST 
                WHERE ALL_LIST.CHECKING_ID='" + CheckingID+"' ";
//            string Sql = @"SELECT CQCI.*,MP.PRODUCT_NAME,MP.PRODUCT_CODE,NG_QTY/RELATED_ITEM_QTY AS NG_RATE 
//                FROM CHK_QUALITY_CHECKING_ITEM CQCI
//                LEFT JOIN PUR_PURCHASE_ARRIVED_ITEM PPAI ON CQCI.RELATED_ITEM_ID=PPAI.ARRIVED_ITEM_ID
//                LEFT JOIN PUR_PURCHASE_ORDER_ITEM PPOI ON PPAI.PURCHASE_ITEM_ID=PPOI.PURCHASE_ITEM_ID
//                LEFT JOIN MST_PRODUCT MP ON PPOI.PRODUCT_ID=MP.PRODUCT_ID
//                WHERE CQCI.CHECKING_ID='" + CheckingID + "'";
            DataSet ds = DataAccess.SelectDataSet(Sql);
            if (ds != null && ds.Tables[0] != null)
                return ds.Tables[0];
            else return null;
        }

        public static DataTable GetDtCheckingItemResultByCheckingID(string CheckingID,string ProcessingFunction)
        {
            if (string.IsNullOrEmpty(CheckingID))
                return null;
            string Sql = @"SELECT CQCI.CHECKING_ITEM_ID,CQCI.RELATED_ITEM_ID,CQCR.QTY,CQCID.DESCRIPTION
                FROM CHK_QUALITY_CHECKING_ITEM CQCI
                LEFT JOIN CHK_QULITY_CHECKING_RESULT CQCR ON CQCI.CHECKING_ITEM_ID=CQCR.CHECKING_ITEM_ID
                INNER JOIN CHK_QUALITY_CHECKING_ITEM_DETAIL CQCID ON CQCR.CHECKING_ITEM_DETAIL_ID=CQCID.CHECKING_ITEM_DETAIL_ID
                WHERE CQCI.CHECKING_ID='" + CheckingID + "'";
            if (string.IsNullOrEmpty(ProcessingFunction.ToString()) == false)
            {
                Sql += " AND CQCR.RESULT='" + ProcessingFunction + "'";
            }
            DataSet ds = DataAccess.SelectDataSet(Sql);
            if (ds != null && ds.Tables[0] != null)
            {
                return ds.Tables[0];
            }
            else return null;
        }
        #endregion
    }
	
	
}	

